﻿Imports System.Data.OleDb
Partial Public Class ShowLoationsByTag
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim conn As New OleDbConnection(ConfigurationManager.ConnectionStrings("PlacesConnectionString").ToString())
        Dim tagCountSQL As String = "SELECT LocationsByTag.TagName, Count(*) AS TagCount FROM(LocationsByTag)GROUP BY LocationsByTag.TagName"
        conn.Open()
        Dim cmd As New OleDbCommand(tagCountSQL, conn)
        Dim tagCountReader = cmd.ExecuteReader()
        Dim tagHash As New Hashtable
        Dim maxTagCount As Integer = 0
        Dim minTagCount As Integer = 0
        If tagCountReader.HasRows() Then

            While tagCountReader.Read()
                'Response.Write(tagCountReader("TagName") & " " & tagCountReader("TagCount"))
                tagHash.Add(tagCountReader("TagName"), CInt(tagCountReader("TagCount")))
                If minTagCount = 0 Then minTagCount = CInt(tagCountReader("TagCount"))
                If minTagCount > CInt(tagCountReader("TagCount")) Then minTagCount = CInt(tagCountReader("TagCount"))
                If maxTagCount < CInt(tagCountReader("TagCount")) Then maxTagCount = CInt(tagCountReader("TagCount"))
            End While
        End If

        Dim fontOptions() As String = {"1em", "2em", "3em", "4em", "4.5em", "5em", "5.5em"}
        Dim scaleUnit As Decimal = (maxTagCount - minTagCount + 1) / Convert.ToDecimal(fontOptions.Length)
        Dim tagCloudHtml As String = ""
        Dim homePageStem As String = "ShowLocationsByTag.aspx?"
        For Each tempTag In tagHash
            Dim scaleValue As Integer = Math.Truncate((tempTag.Value - minTagCount) / scaleUnit)
            tagCloudHtml = tagCloudHtml & String.Format("<A HREF = ""{0}"" style = "" Font-Size:{1};"">{2}</A>", homePageStem & "Tag=" & tempTag.key, fontOptions(scaleValue), tempTag.key) & " "
        Next
        PlaceHolder1.Controls.Add(New LiteralControl(tagCloudHtml))
    End Sub


End Class